(function () {
    var _this = this;
    return {
        countSize: function (size, level) {
            if (size == -1) {
                return "-";
            }
            if (size < 1024 && level == 1) {
                return size + "B";
            }
            var sizeNum = (size / 1024);
            if (sizeNum < 1024) {
                var dw = "";
                if (level == 1) {
                    dw = "KB";
                } else if (level == 2) {
                    dw = "MB";
                } else if (level == 3) {
                    dw = "GB";
                } else if (level == 4) {
                    dw = "TB";
                } else if (level == 5) {
                    dw = "PB";
                } else if (level == 6) {
                    dw = "EB";
                } else if (level == 7) {
                    dw = "BB";
                }
                return (Math.floor(sizeNum * 100) / 100) + dw;
            } else {
                return _this.countSize(sizeNum, level + 1);
            }

        },
        import:[
            {
               module:"module/sysFile/uploadBtn",
            }
        ],
        init:function(){
            var path = _this.params.path;
            _this.data.paths = [];
            if (path != null) {
                var names = path.split("/");
                _this.data.paths = names;
            }
            ajaxGet({
                url:"sysFile/getFolderIdByPath",
                data:{
                    "path": _this.params.path
                },
                success:function(data){
                    _this.data.folderId=data.data;
                    ajaxPost({
                        url: "sysFile/findSysFile",
                        data: {
                            "folderId": _this.data.folderId
                        },
                        success: function (data) {
                            _this.data.sysFileList = data.data;
                            _this.reload();
                        }
                    })
                }
            })
        },
        data:{
            sysFileList:[],
            folderId:null,
            paths:[]
        },
        before: function (data) {
            for (var i = 0; i < data.sysFileList.length; i++) {
                data.sysFileList[i].sizeStr = _this.countSize(data.sysFileList[i].size, 1);
            }
            return data;
        },

        after: function () {
            _this.$(".file-show").hover(function () {
                $(this).addClass("file-show-bg");
            }, function () {
                $(this).removeClass("file-show-bg");
            });
            _this.$(".e-addFolder").click(function () {
                var name = "新建文件夹";
                var nameIndex = 0;
                for (; ;) {
                    var flag = false;
                    for (var i = 0; i < _this.data.sysFileList.length; i++) {
                        if (_this.data.sysFileList[i].name == name) {
                            flag = true;
                        }
                    }
                    if (!flag) {
                        break;
                    } else {
                        nameIndex++;
                        name = "新建文件夹" + nameIndex;
                    }
                }
                var path = _this.params.path;
                _this.data.paths = [];
                if (path != null) {
                    var names = path.split("/");
                    _this.data.paths = names;
                }

                ajaxPost({
                    url: "sysFile/addFolder",
                    data: {
                        "name": name,
                        "folderId": _this.data.folderId
                    },
                    success: function (data) {
                        _this.data.sysFileList.unshift(data.data);
                        _this.data.renameId = data.data.id;
                        _this.reload();
                    }
                })
            });

            _this.$(".e-rename").click(function () {
                var id = $(this).attr("data-id");
                _this.data.renameId = id;
                _this.reload();
            });

            _this.$("#name" + _this.data.renameId).focus();

            _this.$(".e-rename-finish").click(function () {
                var id = $(this).attr("data-id");
                var name = _this.$("#name" + _this.data.renameId).val();
                ajaxPost({
                    url: "sysFile/rename",
                    data: {
                        "name": name,
                        "id": id,
                    },
                    success: function (data) {
                        for (var i = 0; i < _this.data.sysFileList.length; i++) {
                            if (data.data.id == _this.data.sysFileList[i].id) {
                                _this.data.sysFileList[i] = data.data;
                                _this.data.renameId = null;
                                _this.reload();
                            }
                        }
                    }
                })
            });

            _this.$(".e-delete").click(function(){
                var id = $(this).attr("data-id");
                swal({
                    title: "您确定要删除这条数据吗",
                    text: "删除后将无法恢复，请谨慎操作！",
                    type: "warning",
                    showCancelButton: true,
                    confirmButtonColor: "#DD6B55",
                    confirmButtonText: "删除",
                    closeOnConfirm: true,
                    cancelButtonText: "取消"
                }, function () {
                    ajaxGet({
                        url: "sysFile/delete",
                        data: {id: id},
                        success: function (data) {
                            toastr.success("删除成功");
                            _this.init();
                        }
                    })
                });
            });

            _this.$(".e-file-show").click(function () {
                var name = $(this).attr("data-name");
                var path = _this.params.path == null ? name : _this.params.path + "/" +encodeURIComponent(name);
                DsModule.to("module/sysFile/index", {path: path},true);
            });



            _this.$(".e-get-file").click(function(){
                var id = $(this).attr("data-id");
                var data=_this.toMap(".data-param");
                var exportForm = $("<form target='_blank' action='"+window.api+"sysFile/"+id+"?token="+window.sysUser.token+"' method='post' style='display: none'></form>")
                exportForm.append("<input type='hidden' name='token' value='"+window.sysUser.token+"'/>")
                exportForm.append("<input type='hidden' name='projectName' value='"+data.projectName+"'/>")
                exportForm.append("<input type='hidden' name='packageName' value='"+data.packageName+"'/>")
                $(document.body).append(exportForm);
                exportForm.submit();
                exportForm.remove();
            });

            var upload=new Module("module/sysFile/uploadBtn");
            upload.params.folderId=_this.data.folderId;
            upload.params.uploadSuccess=function(){
                _this.init();
            }
            upload.load("uploadFile");
        }
    }
})